﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaBrowser.Library.Persistance;
using MediaBrowser.Library.Entities;
using MusicPlugin.LibraryManagement;
using MediaBrowser.Library.Logging;
//using MusicPlugin.ThemeModelItemInterfaces;

namespace MusicPlugin.Library.Entities
{
    public class ArtistAlbum : Folder//, IArtistAlbumModelItem
    {
        DateTime lastBuildInformation = DateTime.MinValue;
        //[Persist]
        List<string> _artists;
        public List<string> Artists { get { BuildInformation(); return _artists; } }
        //[Persist]
        List<string> _albums;
        public List<string> Albums { get { BuildInformation(); return _albums; } }
        //[Persist]
        List<string> _artistAlbums;
        public List<string> ArtistAlbums { get { BuildInformation(); return _artistAlbums; } }
        //[Persist]
        List<string> _genres;
        public List<string> Genres { get { BuildInformation(); return _genres; } }
        //[Persist]
        int _duration;
        public int DurationInt { get { BuildInformation(); return _duration; } }
        
        public string Duration
        {
            get { return MusicHelper.SecondsToHighestTwoUnits(DurationInt); }
        }

        public void BuildInformation()
        {            
            //stop recalc of information to often
            if (DateTime.Now.Subtract(lastBuildInformation).TotalSeconds <= 30)
                return;

            Logger.ReportInfo("MusicPlugin going to build information for something.");

            lastBuildInformation = DateTime.Now;

            _duration = 0;
            CreateOrClearLists();
            base.ValidateChildren();
            
            foreach (var item in ActualChildren)
            {
                if (item is Song)
                {
                    MusicHelper.AddToList((item as Song).Album, Albums);
                    MusicHelper.AddToList((item as Song).Artist, Artists);
                    MusicHelper.AddToList((item as Song).Genres, Genres);
                    MusicHelper.AddToList((item as Song).Artist," - ",(item as Song).Album, ArtistAlbums);
                    _duration += ((item as Song).DurationInt);
                }
                else if (item is ArtistAlbum)
                {
                    MusicHelper.AddToList((item as ArtistAlbum).Albums, Albums);
                    MusicHelper.AddToList((item as ArtistAlbum).Artists, Artists);
                    MusicHelper.AddToList((item as ArtistAlbum).Genres, Genres);
                    MusicHelper.AddToList((item as ArtistAlbum).ArtistAlbums, ArtistAlbums);
                    _duration += ((item as ArtistAlbum).DurationInt);
                }                
            }
            //TestWriter((item as ArtistAlbum).Albums);
            //TestWriter((item as ArtistAlbum).Artists);
            //TestWriter((item as ArtistAlbum).Genres);
            //TestWriter((item as ArtistAlbum).ArtistAlbums);

            lastBuildInformation = DateTime.Now;
        }

        void TestWriter(List<string> list)
        {
            if (list != null)
                Logger.ReportInfo(string.Join(",", list.ToArray()));
        }

        private void CreateOrClearLists()
        {
            if (_albums == null)
                _albums = new List<string>();
            else
                _albums.Clear();

            if (_artistAlbums == null)
                _artistAlbums = new List<string>();
            else
                _artistAlbums.Clear();

            if (_genres == null)
                _genres = new List<string>();
            else
                _genres.Clear();

            if (_artists == null)
                _artists = new List<string>();
            else
                _artists.Clear();

        }
    }
}
